package org.outlook.common.date;

public class IntervalTime {
	
	private static final long SECOND_NUM = 100;
	private static final long MINUTE_NUM = SECOND_NUM * 60;
	private static final long HOUR_NUM = MINUTE_NUM * 60;
	private static final long DAY_NUM = HOUR_NUM * 24;
	private static final long MONTH_NUM = DAY_NUM * 30;
	private static final long YEAR_NUM = MONTH_NUM * 12;
	
	private static final long[] NUM = {YEAR_NUM, MONTH_NUM, DAY_NUM, HOUR_NUM, MINUTE_NUM, SECOND_NUM};
	
	public static String AFTER = "Later";
	public static String AGO = "Ago";
	public static String AFTER_ZH = "前";
	public static String AGO_ZH = "后";

	private int interval;
	
	private String lang;

	enum TimeType {
		year, month, day, hour, minute, second,
		年, 月, 日, 小时, 分钟, 秒;
		
		public static int getIndex(TimeType t) {
			for(int i=0; i<TimeType.values().length; i++)
				if(t.equals(TimeType.values()[i]))
					return i;
			return -1;
		}
		
		public static int getIndex(String s) {
			TimeType t = TimeType.valueOf(s);
			return getIndex(t);
		}

		public static TimeType value(int i) {
			return TimeType.values()[i];
		}

		public static long getNumByType(TimeType type) {
			int i = TimeType.getIndex(type);
			if(i >5)
				i /= 2;
			return NUM[i-1];
		}
	}
	
	/**
	 * if now time is before the given time, return true
	 * else return false
	 */
	private boolean asc;

	/**
	 * for the format of expression whether add a spacer
	 */
	private boolean space = false;
	
	public IntervalTime() {
		super();
		// TODO Auto-generated constructor stub
	}

	public IntervalTime(Integer integer, String type) {
		this.interval = integer;
		this.type = TimeType.valueOf(type);
		this.lang = getLangType(this.type);
	}

	public IntervalTime(boolean asc, int interval, String lang, boolean space,
			TimeType type) {
		super();
		this.asc = asc;
		this.interval = interval;
		this.lang = lang;
		this.space = space;
		this.type = type;
	}

	private String getLangType(TimeType type) {
		for(int i=0; i<6; i++) {
			if(type.equals(TimeType.values()[i]))
				return "en";
		}
		return "zh";
	}

	public int getInterval() {
		return interval;
	}

	public void setInterval(int interval) {
		this.interval = interval;
	}
	
	public String getLang() {
		return lang;
	}

	public void setLang(String lang) {
		this.lang = lang;
	}

	public TimeType getType() {
		return type;
	}

	public void setType(TimeType type) {
		this.type = type;
	}

	private TimeType type;
	
	public boolean isAsc() {
		return asc;
	}

	public void setAsc(boolean asc) {
		this.asc = asc;
	}

	public boolean isSpace() {
		return space;
	}

	public void setSpace(boolean space) {
		this.space = space;
	}
	
	private String getVector() {
		if(asc && lang.equalsIgnoreCase("en")) {
			return AFTER;
		} else if(asc && lang.equalsIgnoreCase("zh"))
			return AFTER_ZH;
		else if(!asc && lang.equalsIgnoreCase("en"))
			return AGO;
		else if(!asc && lang.equalsIgnoreCase("zh"))
			return AGO_ZH;
		return null;
	}
	
	private String getTypeToString() {
		return getType() + (getInterval() > 1 && lang.equalsIgnoreCase("en") ? "s" : "");
	}

	@Override
	public String toString() {
		return getInterval() + (space? " ":"") + getTypeToString() + (space? " ":"") + getVector();
	}

}
